package cn.com.dhcc.turbo.dao.exch;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.AbstractCrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.CollectionUtil;
import cn.com.dhcc.turbo.entity.exch.SendFileData;

@Repository
public class SendFileDataDao extends AbstractCrudDao<SendFileData> {
    @SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);

    /**
     * 批量更新数据发送状态
     *
     * @param dataList
     */
    public int updateSendStatus(List<SendFileData> dataList, String status) throws DaoException {
        String msg = "";
        try {
            String updateSql = "update " + SI_TABLE("TURBO_SEND_FILE_DATA") + " set send_status = ?,routelog=?,modified=?,SENDER_SIGNED =? ,RECIVER_SIGNED=?,THROUGH_GAP=?,POSTHEAD=? where order_id = ?";
            List<Object[]> paramList = new ArrayList<Object[]>();
            if (CollectionUtil.hasElement(dataList)) {
                for (SendFileData vo : dataList) {
                    Object[] param = new Object[8];
                    param[0] = status;
                    param[1] = vo.getRoutelog();
                    param[2] = new Date();
                    param[3] = vo.getSenderSigned();
                    param[4] = vo.getReciverSigned();
                    
                    param[5] = vo.getThroughGap();
                	param[6] = vo.getPosthead();
                	
                    param[7] = vo.getOrderId();
                    paramList.add(param);
                }
            }
            return CM.getDao().batchUpdate("批量修改日志状态成功", updateSql, paramList);
        } catch (DaoException e) {
            msg = "批量更新数据发送状态失败";
            logger.error(msg, e);
            throw new DaoException(msg, e);
        }
    }

    /**
     * 批量更新数据发送状态
     *
     * @param dataList
     */
    public int updateNotifyStatus(List<SendFileData> dataList, String status) throws DaoException {
        String msg = "";
        try {
            String updateSql = "update " + SI_TABLE("TURBO_SEND_FILE_DATA") + " set notify_status = ?,routelog=?,modified=?,SENDER_SIGNED =? ,RECIVER_SIGNED=? ,THROUGH_GAP=?,POSTHEAD=? where order_id = ?";
            List<Object[]> paramList = new ArrayList<Object[]>();
            if (CollectionUtil.hasElement(dataList)) {
                for (SendFileData vo : dataList) {
                    Object[] param = new Object[8];
                    param[0] = status;
                    param[1] = vo.getRoutelog();
                    param[2] = new Date();
                    param[3] = vo.getSenderSigned();
                    param[4] = vo.getReciverSigned();
                    
                    param[5] = vo.getThroughGap();
                	param[6] = vo.getPosthead();
                	
                    param[7] = vo.getOrderId();
                    paramList.add(param);
                }
            }
            return CM.getDao().batchUpdate("批量修改日志状态成功", updateSql, paramList);
        } catch (DaoException e) {
            msg = "批量更新数据发送状态失败";
            logger.error(msg, e);
            throw new DaoException(msg, e);
        }
    }

    public String getSendCount(String adaptercode, Date begintime, Date endtime) throws DaoException {
        String msg = "";
        try {
            String updateSql = "select count(*) from " + SI_TABLE("TURBO_SEND_FILE_DATA") + " where send_adaptorcode =? and  CREATED >=? and CREATED<?";
            Object[] paramList = new Object[]{adaptercode, begintime, endtime};
            return CM.getDao().queryForInt("查询‘发送数量’成功", updateSql, paramList) + "";
        } catch (DaoException e) {
            msg = "查询‘发送数量’失败";
            logger.error(msg, e);
            throw new DaoException(msg, e);
        }
    }

    public String getSendWait(String adaptercode) throws DaoException {
        String msg = "";
        try {
            String updateSql = "select count(*) from " + SI_TABLE("TURBO_SEND_FILE_DATA") + " where send_adaptorcode =? and SEND_STATUS='0'";
            Object[] paramList = new Object[]{adaptercode};
            return CM.getDao().queryForInt("查询‘等待发送数量’成功", updateSql, paramList) + "";
        } catch (DaoException e) {
            msg = "查询‘等待发送数量’失败";
            logger.error(msg, e);
            throw new DaoException(msg, e);
        }
    }

    public String getSendSending(String adaptercode) throws DaoException {
        String msg = "";
        try {
            String updateSql = "select count(*) from " + SI_TABLE("TURBO_SEND_FILE_DATA") + " where send_adaptorcode =? and  SEND_STATUS in (11,12,21,22,31,32,41,42,43,51,52)";
            Object[] paramList = new Object[]{adaptercode};
            return CM.getDao().queryForInt("查询‘发送中数量’成功", updateSql, paramList) + "";
        } catch (DaoException e) {
            msg = "查询‘发送中数量’失败";
            logger.error(msg, e);
            throw new DaoException(msg, e);
        }
    }

    public String getSendSuccess(String adaptercode) throws DaoException {
        String msg = "";
        try {
            String updateSql = "select count(*) from " + SI_TABLE("TURBO_SEND_FILE_DATA") + " where send_adaptorcode =? and  SEND_STATUS in (1,33)";
            Object[] paramList = new Object[]{adaptercode};
            return CM.getDao().queryForInt("查询‘发送成功数量’成功", updateSql, paramList) + "";
        } catch (DaoException e) {
            msg = "查询‘发送成功数量’失败";
            logger.error(msg, e);
            throw new DaoException(msg, e);
        }
    }

    public String getSendFail(String adaptercode) throws DaoException {
        String msg = "";
        try {
            String updateSql = "select count(*) from " + SI_TABLE("TURBO_SEND_FILE_DATA") + " where send_adaptorcode =? and  SEND_STATUS='3'";
            Object[] paramList = new Object[]{adaptercode};
            return CM.getDao().queryForInt("查询‘发送失败数量’成功", updateSql, paramList) + "";
        } catch (DaoException e) {
            msg = "查询‘发送失败数量’失败";
            logger.error(msg, e);
            throw new DaoException(msg, e);
        }
    }

    

	/**
	 * 获取发送节点列表
	 * @return
	 */
	public List<Map<String, Object>> getSendNodeList() throws DaoException {
		String msg = "";
		try {
			String sql = "SELECT code,case when node_name is null then code else node_name end  name FROM( "+
					"SELECT DISTINCT send_nodecode code FROM "+SI_TABLE("turbo_send_file_data")+" )a  "+
					"left join "+SI_TABLE("turbo_node_info")+" b ON a.code = b.node_code ";
			String[] args = new String[] { };
			return CM.getDao().listMap("获取发送节点列表", sql, args);
		} catch (DaoException e) {
			msg = "获取发送节点列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	
	/**
	 * 获取接收节点列表
	 * @return
	 */
	public List<Map<String, Object>> getRecvNodeList() throws DaoException {
		String msg = "";
		try {
			String sql = "SELECT code,case when node_name is null then code else node_name end  name FROM( "+
					"SELECT DISTINCT recv_nodecode code FROM "+SI_TABLE("turbo_send_file_data")+" )a  "+
					"left join "+SI_TABLE("turbo_node_info")+" b ON a.code = b.node_code ";
			String[] args = new String[] {};
			return CM.getDao().listMap("获取发送节点列表", sql, args);
		} catch (DaoException e) {
			msg = "获取接收节点列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	/**
	 * 获取发送适配器列表
	 * @return
	 */
	public List<Map<String, Object>> getSendAdapterList() throws DaoException {
		String msg = "";
		try {
			String sql = "SELECT code,case when ADAPTER_NAME is null then code else ADAPTER_NAME end name FROM( "+
					"SELECT DISTINCT SEND_ADAPTORCODE code FROM "+SI_TABLE("turbo_send_file_data")+" )a  "+
					"left join "+SI_TABLE("turbo_adapter_base")+" b ON a.code = b.ADAPTER_CODE ";
			String[] args = new String[] {};
			return CM.getDao().listMap("获取发送节点列表", sql, args);
		} catch (DaoException e) {
			msg = "获取接收节点列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
}
